package com.pizzaria.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Util {
	private static DateFormat dateFormatSimple;
	private static NumberFormat numberFormat;
	private static DateFormat datetTimeFormatSimple;
	
	static{
		Locale l = new Locale("pt","BR");
		numberFormat = new DecimalFormat(",###,##0.00", new DecimalFormatSymbols(l));
		dateFormatSimple = new SimpleDateFormat("dd/MM/yyyy", l);
		datetTimeFormatSimple = new SimpleDateFormat("dd/MM/yyyy hh:mm", l);
	}
	
	public static Date parseDate(String d){
		try {
			return dateFormatSimple.parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String formatDate(Date d){
		return dateFormatSimple.format(d);
	}
	
	public static String formatDateTime(Date d){
		return datetTimeFormatSimple.format(d);
	}
	
	public static String formatDecimal(float f){
	    return numberFormat.format(f);
	}

	public static float parseFloat(String f) {
		if(f == null || f.isEmpty()) return 0;
		
		return Float.parseFloat(f.replaceAll("\\.", "").replaceAll(",", "."));
	}
}
